package com.chen.dao;

import com.chen.entity.UserEntity;
import com.chen.utils.ChenJdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class UserDao {
    public ArrayList<UserEntity> getByAll(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        try {
            // 1.获取连接
            connection = ChenJdbcUtils.getConnection();
            //2.获取执行者对象
            preparedStatement = connection.prepareStatement("select * from chen_user where is_delete = 0");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String  pwd = resultSet.getString("pwd");
                String phone = resultSet.getString("phone");
                Integer isDelete = resultSet.getInt("is_delete");
                UserEntity userEntity = new UserEntity(id, name, pwd, phone, isDelete);
                userEntities.add(userEntity);
            }
            return userEntities;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 释放资源
            ChenJdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }
    }

    public UserEntity getByPhoneUser(String phone){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = ChenJdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement("select * from chen_user where phone=? AND is_delete=0");
            preparedStatement.setString(1, phone);
            resultSet = preparedStatement.executeQuery();
            if(!resultSet.next()){
                return null;
            }
            Integer id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String pwd = resultSet.getString("pwd");
            String dbPhone = resultSet.getString("phone");
            UserEntity userEntity = new UserEntity(id, name, pwd, dbPhone, 0);
            return userEntity;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            ChenJdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }
    }

    public int updateDeleteByPhone(String phone){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取连接
            connection = ChenJdbcUtils.getConnection();
            // 2.开启事务
            ChenJdbcUtils.beginTransaction(connection);
            //3.获取执行者对象
            preparedStatement = connection.prepareStatement(
                    "UPDATE `chen`.`chen_user` SET `is_delete` = 1 WHERE `phone` = ?");
            preparedStatement.setString(1, phone);
            int result = preparedStatement.executeUpdate();
            ChenJdbcUtils.commitTransaction(connection);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ChenJdbcUtils.rollBackTransaction(connection);
            return 0;
        } finally {
            // 释放资源
            ChenJdbcUtils.closeConnection(preparedStatement, connection);
        }
    }

    public int insertUser(UserEntity userEntity) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取连接
            connection = ChenJdbcUtils.getConnection();
            // 2.开启事务
            ChenJdbcUtils.beginTransaction(connection);
            //3.获取执行者对象
            preparedStatement = connection.prepareStatement(
                    "INSERT INTO `chen`.`chen_user` (`id`, name, pwd, phone," +
                            " `is_delete`)" +
                            " VALUES (null, ?, ?, ?, 0);\n");
            preparedStatement.setString(1, userEntity.getName());
            preparedStatement.setString(2, userEntity.getPwd());
            preparedStatement.setString(3, userEntity.getPhone());
            int result = preparedStatement.executeUpdate();
            ChenJdbcUtils.commitTransaction(connection);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ChenJdbcUtils.rollBackTransaction(connection);
            return 0;
        } finally {
            // 释放资源
            ChenJdbcUtils.closeConnection(preparedStatement, connection);
        }
    }
}
